package view;

import connectivity.QueryManager;
import java.awt.Color;
import main.BarApplication;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import model.Category;

public class CategoryOverview extends JPanel implements MouseListener {
    private final int verticalPosition = 60;
    private final int offset = 40;
    private LoginScreen login;

    public CategoryOverview(LoginScreen login) {
        super();
        setLayout(null);
        initComponents();
        this.login = login;
        
    }

    /** create the gui for this page */
    private void initComponents() {
        this.setBackground(Color.white);
        addTitle();
        addcategoryItems();
        addBasket();
    }

    /** add the page's title */
    private void addTitle() {


        JLabel lblTitle = new JLabel();
        lblTitle.setText("Kies een categorie (1/4)");
        lblTitle.setBounds(20, 20, 300, 20);
        lblTitle.setFont(BarApplication.FONT_16_BOLD);
        add(lblTitle);

    
    }

    /** add the different catergories to the page */
    private void addcategoryItems() {
        QueryManager queryManager = BarApplication.getQueryManager();
        List<Category> categories = queryManager.getCategories();

        for (int i = 0; i < categories.size(); i++) {
            Category category = categories.get(i);
            JLabel lblDot = new JLabel("\u2022");
            lblDot.setFont(BarApplication.FONT_12_BOLD);
            lblDot.setBounds(20, verticalPosition + i * offset, 10, 20);
            add(lblDot);

            JLabel lblCategorie = new JLabel();
            lblCategorie.setName(String.valueOf(category.getCategoryId()));
            lblCategorie.setText(category.getName());
            lblCategorie.setBounds(35, verticalPosition + i * offset, 200, 20);
            lblCategorie.setFont(BarApplication.FONT_12_BOLD);
            lblCategorie.addMouseListener(this);
            lblCategorie.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            add(lblCategorie);

            JLabel lblDescription = new JLabel();
            lblDescription.setText(category.getDescription());
            lblDescription.setBounds(35, verticalPosition  + i * offset + 15, 200, 20);
            lblDescription.setFont(BarApplication.FONT_10_PLAIN);
            add(lblDescription);
        }
    }

    /** add the basket view into this page */
    private void addBasket() {
        BasketDisplay basket = new BasketDisplay();
        this.add(basket);
    }

    @Override
    public void mouseClicked(MouseEvent event) {
        JLabel label = (JLabel) event.getSource();
        int categoryId = Integer.parseInt(label.getName());
        
        ProductList productList = new ProductList(categoryId);
        shopping_bar shopping = new shopping_bar(login);
        shopping.jPanel_ProductList.add(productList);
        BarApplication.getInstance().showPanel(shopping);
    }

    @Override
    public void mouseEntered(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mousePressed(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseReleased(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void mouseExited(MouseEvent event) {
        // Intentionally left blank.
    }

    @Override
    public void paint(Graphics graphics) {
        super.paint(graphics);
        graphics.drawLine(20, 45, 540, 45);
    }
}
